package me.zhengjie.modules.lock.impl;

import com.zelk.lock.api.clent.feign.LockDeviceBleKeyApi;
import com.zelk.lock.api.dto.LockBleKeyListDTO;
import com.zelk.lock.api.vo.LockBleKeyListReqVO;
import com.zelk.smartlock.common.response.ResponsePage;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.lock.mapping.MappingUtil;
import me.zhengjie.modules.lock.service.LockDeviceBleKeyService;
import me.zhengjie.modules.lock.service.dto.LockBleKeyInfoListDTO;
import me.zhengjie.modules.lock.service.vo.LockBleKeyInfoListReqVO;
import me.zhengjie.modules.lock.util.RequestCall;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LockDeviceBleKeyServiceImpl implements LockDeviceBleKeyService {
    @Autowired
    private MappingUtil mappingUtil;
    @Autowired
    private LockDeviceBleKeyApi lockDeviceBleKeyApi;

    @Override
    public Map<String, Object> selectBleKeyList(LockBleKeyInfoListReqVO reqVO, Pageable pageable) {
        LockBleKeyListReqVO listReqVO = mappingUtil.toLockBleKeyListReqVO(reqVO);
        listReqVO.setPageIdx((long) pageable.getPageNumber());
        listReqVO.setPageSize((long) pageable.getPageSize());
        ResponsePage<List<LockBleKeyListDTO>> bleKeyListPage = RequestCall.executePage(lockDeviceBleKeyApi::selectBleKeyListPage, listReqVO, null);
        List<LockBleKeyListDTO> deviceList = Optional.ofNullable(bleKeyListPage.getData()).orElse(new ArrayList<>());
        List<LockBleKeyInfoListDTO> deviceInfoListDTOS = deviceList.stream().map(v -> {
            LockBleKeyInfoListDTO bleKeyInfoDTO = mappingUtil.toLockBleKeyInfoListDTO(v);
            //TODO 还要填充商户名称
            return bleKeyInfoDTO;
        }).collect(Collectors.toList());
        return PageUtil.toPage(deviceInfoListDTOS, bleKeyListPage.getTotal());
    }
}
